package com.difsonapps.android.marker.beans;

import org.json.JSONException;
import org.json.JSONObject;

import com.difsonapps.android.marker.AppConstant;
import com.difsonapps.android.marker.util.MarkerUtil;

import android.os.Parcel;
import android.os.Parcelable;

public class Photo  implements Parcelable{
	private int id, nbMarks=0, nbLikes=0, originalId=0;
	private double avgMarks = 0;
	
	private boolean iLike = false;
    private String title, description, image, timestamp, created_at, modified_at;
	
    public Photo() {
		super();
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getNbMarks() {
		return nbMarks;
	}

	public void setNbMarks(int nbMarks) {
		this.nbMarks = nbMarks;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getImage() {
		return image;
	}

	public void setImage(String image) {
		this.image = image;
	}

	public String getTimestamp() {
		return timestamp;
	}
	

	public String getCreated_at() {
		return created_at;
	}

	public void setCreated_at(String created_at) {
		this.created_at = created_at;
	}

	public String getModified_at() {
		return modified_at;
	}

	public void setModified_at(String modified_at) {
		this.modified_at = modified_at;
	}

	public void setTimestamp(String timestamp) {
		this.timestamp = timestamp;
	}
	
	

	public double getAvgMarks() {
		return avgMarks;
	}

	public void setAvgMarks(double avgMarks) {
		this.avgMarks = avgMarks;
	}
	
	

	public int getNbLikes() {
		return nbLikes;
	}

	public void setNbLikes(int nbLikes) {
		this.nbLikes = nbLikes;
	}

	public boolean isiLike() {
		return iLike;
	}

	public void setiLike(boolean iLike) {
		this.iLike = iLike;
	}

	public int getOriginalId() {
		return originalId;
	}

	public void setOriginalId(int originalId) {
		this.originalId = originalId;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Photo other = (Photo) obj;
		if (id != other.id)
			return false;
		return true;
	}

	public Photo(Parcel in){
		this();
		this.id = in.readInt();
		this.nbMarks = in.readInt();
		this.title = in.readString();
		this.description = in.readString();
		this.image = in.readString();
		this.timestamp = in.readString();
		this.created_at = in.readString();
		this.modified_at = in.readString();
		this.avgMarks = in.readDouble();
		this.nbLikes = in.readInt();
		this.iLike = in.readInt()==1;
		this.originalId = in.readInt();
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeInt(id);
		dest.writeInt(nbMarks);
		dest.writeString(title);
		dest.writeString(description);
		dest.writeString(image);
		dest.writeString(timestamp);
		dest.writeString(created_at);
		dest.writeString(modified_at);
		dest.writeDouble(avgMarks);
		dest.writeInt(nbLikes);
		dest.writeInt(iLike?1:0);
		dest.writeInt(originalId);
	}	
	
	public static final Parcelable.Creator<Photo> CREATOR = new
		Parcelable.Creator<Photo>() {
			@Override
			public Photo createFromParcel(Parcel source) {
				return new Photo(source);
			}
			@Override
			public Photo[] newArray(int size) {
				return new Photo[size];
		}
	};

	@Override
	public int describeContents() {
		return 0;
	}
	
	public static Photo parseFromJsonObject(JSONObject json){
		if(json==null || !json.has("id")) return null;
		Photo result = new Photo();
		try {
			result.setId(json.getInt("id"));
			result.setNbMarks(json.getInt("nb_marks"));
			result.setAvgMarks(json.has("avg_marks")?json.getDouble("avg_marks"):0);
			result.setNbLikes(json.has("nb_likes")?json.getInt("nb_likes"):0);
			result.setOriginalId(json.has("original_id")?json.getInt("original_id"):0);
			result.setiLike(json.has("i_like")?json.getBoolean("i_like"):false);
			result.setTitle(json.getString("name"));
			String image = json.getString("image");
			result.setImage(MarkerUtil.buildImageUrl(image, AppConstant.IMAGES_FOLDER, null));
			result.setDescription(json.getString("desc"));
			result.setTimestamp(json.getString("timestamp"));
			result.setCreated_at(json.getString("created_at"));
			result.setModified_at(json.getString("modified_at"));
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}
		return result;
	}
    

}
